//边输入边校验
$(document).ready(function(){ 

	//$("#customer_type_product").focus(); 

	var flag0=true;
	var flag1=true;
	var flag2=true;
	var flag3=true;
	var flag4=true;
	var flag5=true;
	var flag6=true;
	var flag7=true;
	$("#customer_type_product").blur(function(){
		if(this.value=="-1"){
			$(this).focus();
			if(flag0==true){
				$("#customer_type_product-button").after("<div id ='add0' class='red'>请选择客户需求产品</div>");
				flag0=false;
			}
		}else {
			$("#add0").remove();
			flag0=true;
			$("#provider_phone_number").focus();
			
		}
	});
	$("#provider_phone_number").blur(function(){
		if(!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.value))){
			$(this).focus();
			if(flag6==true){
				$(".ui-input-text").eq(0).after('<div id ="add6" class="red">请输入真实手机号码</div>');
				flag6=false;
			}
		}else {
			$("#add6").remove();
			flag6=true;
			
		}
	});

	$("#project_name").blur(function(){
		//项目名称校验
		if(this.value.length<8){
			$(this).focus();
			if(flag7==true){
				$(".ui-input-text").eq(2).after('<div id ="add7" class="red">项目名称不能少于8个字符</div>');
				flag7=false;
			}
		}else {
			$("#add7").remove();
			flag7=true;
			
		}
	});
	$("#selectProvince").blur(function(){
		if(this.value==-1){
			$(this).focus();
			if(flag1==true){
				$("#selectProvince-button").after("<div id ='add1' class='red'>请选择省份</div>");;
				flag1=false;
			}
		}else {
			$("#add1").remove();
			flag1=true;
			$("#selectCity").focus();
		}
	
	});
	$("#selectCity").blur(function(){
		if(this.value==-1){
			$(this).focus();
			if(flag2==true){
				$("#selectCity-button").after("<div id ='add2' class='red'>请选择省份</div>");;
				flag2=false;
			}
			
		}else {
			$("#add2").remove();
			flag2=true;
		}

	});
	$("#project_start_time").blur(function(){
		if(!(/\S/.test(this.value))){
			this.focus();
			$(this).attr('placeholder','请选择潜在项目启动时间！');
			$(this).addClass("invalid"); 
		}

	});
	$("#scale").blur(function(){
		if(this.value ==-1){
			$(this).focus();
			if(flag3==true){
				$("#scale-button").after("<div id ='add3' class='red'>请选择项目预估规模</div>");;
				flag3=false;
			}
		//	return false;
		}else{
			$("#add3").remove();
			flag3=true;
			$("#customer_type").focus();
		}

	});
	$("#customer_type").blur(function(){
		if(this.value ==-1){
			$(this).focus();
			if(flag4==true){
				$("#customer_type-button").after("<div id ='add4' class='red'>请选择所属行业</div>");;
				flag4=false;
			}
		//	return false;
		}else{
			$("#add4").remove();
			flag4=true;
			//$("#info").focus();
		}

	});
	$("#info").blur(function(){
		$("#truth_type").focus();
	});
	$("#truth_type").blur(function(){
		if(this.value ==-1){
			$(this).focus();
			if(flag5==true){
				$("#truth_type-button").after("<div id ='add5' class='red'>请选择项目真实性</div>");;
				flag5=false;
			}
		//	return false;
		}else{
			$("#add5").remove();
			flag5=true;
		}
	});
});
var a=/^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/;//一个正则表达式，只含有汉字、数字、字母、下划线不能以下划线开头和结尾：
//整体提交按钮事件
function all_submit(){
	var provider_phone_number = $("input[name='provider_phone_number']").val();
	//手机号校验
	if(!(/\S/.test(provider_phone_number))){
		alert("手机号不能为空");  
		$("#provider_phone_number").focus();
		return false;
	}else if(!(/^[1][3,4,5,6,7,8][0-9]{9}$/.test(provider_phone_number))){ 
		alert("手机号码有误，请重填");  
		$("#provider_phone_number").focus();
		return false; 
	}
	var provider_name = $("input[name='provider_name']").val();
	//姓名校验
//	if(!(/\S/.test(provider_name))){
//		alert("姓名不能为空");  
//		$("#provider_name").focus();
//		return false;
//	}
	
	var project_name = $("input[name='project_name']").val();
	//项目名称校验
	if(!(/\S/.test(project_name))){
		alert("项目名称不能为空");  
		$("#project_name").focus();
		return false;
	}
	//if(!(a.test(provider_name))){
	//	alert("项目名称只含有汉字、数字、字母、下划线");  
	//	$("#provider_name").focus();
	//	return false;
	//}
	if(project_name.length<8){
		alert("项目名称不能少于8个字符");  
		$("#project_name").focus();
		return false;
	}
	var provid = $("#selectProvince").val();
	if(provid==-1){
		alert("请选择省份");return false;
	}
	var cityid =$("#selectCity").val();
	if(cityid==-1){
		alert("请选择市");return false;
	}
	var project_start_time =$("input[name='project_start_time']").val();
	//潜在项目启动时间校验
	if(!(/\S/.test(project_start_time))){
		alert("请选择潜在项目启动时间！");  
		$("#project_start_time").focus();
		return false;
	}

	var scale =$("select[name='scale']").val();
	if(scale ==-1){
		alert("请选择预估规模");return false;
	}
	var customer_type =$("select[name='customer_type']").val();
	if(customer_type ==-1){
		alert("请选择客户类型");return false;
	}
	var communic =$("#communic").prop('checked'); // alert(communic);
	if(communic==true){
		communic="尽快与我取得联系";
	}else {
		communic="";
	}
	var channel =$("#channel").prop('checked'); //alert(channel);
	if(channel==true){
		channel="我想成为（推荐）渠道商";
	}else {
		channel="";
	}
	//return ;

	var truth_type =$("#truth_type").val(); 
	if(truth_type==-1){
		alert("请选择项目真实性");return false;
	}
	var file_url ="";
	if(truth_type ==0 || truth_type ==1 || truth_type ==3){
		 file_url =$("input[name='file_url']").val();
		 if(file_url==""){
				alert("请上传附件");return false;
		 }
	}else{
		file_url = $("#file_url1").val();
		if(file_url==""){
			alert("请填入甲方姓名和联系方式");return false;
		}
	}
	var openid =$("input[name='openid']").val();//alert(openid);
	if(openid ==""){
		alert("请重新授权");return false;
	}
	var zf_openid = $("#zf_openid").val();
	
	var project_description =$("textarea[name='project_description']").val();
	var customer_type_product = $("#customer_type_product").val();//客户需求产品
	if(customer_type_product=="-1"){
		alert("请选择客户需求产品");
		$("#customer_type_product").focus();
		return false;
	}
	showLoader();
	$.ajax({
		type: "post",
		url: "rest/msgenter",
		data: {
			"provider_name":provider_name,
			"provider_phone_number":provider_phone_number,
			"project_name":project_name,
			"provid":provid,
			"cityid":cityid,
			"project_start_time":project_start_time,
			"scale":scale,
			"customer_type":customer_type,
			"communic":communic,
			"truth_type":truth_type,
			"file_url":file_url,
			"openid":openid,
			"project_description":project_description,
			"channel":channel,
			"customer_type_product":customer_type_product,
			"zf_openid":zf_openid
		},
		dataType: "json",
		success: function (data) {
			hideLoader()  ;
			if(data.status == "3"){
				alert("不可提交，请退出重新登录。");
			}else if(data.status == "2"){
				alert("这个项目已存在，提交失败。");
			}else if(data.status == "1"){
				var like_name=data.like_name;
			    var like_value=data.like_value;
			    
				if(confirm("项目可能存在，建议更改项目，是否继续提交？")){
					$.ajax({
						type: "post",
						url: "rest/mes",
						data: {
							"provider_name":provider_name,
							"provider_phone_number":provider_phone_number,
							"project_name":project_name,
							"provid":provid,
							"cityid":cityid,
							"project_start_time":project_start_time,
							"scale":scale,
							"customer_type":customer_type,
							"communic":communic,
							"file_url":file_url,
							"truth_type":truth_type,
							"openid":openid,
							"project_description":project_description,
							"channel":channel,
							"customer_type_product":customer_type_product,
							"like_value":like_value,
							"like_name":like_name,
							"zf_openid":zf_openid
						},
						dataType: "json",
						success: function (dt) {
							if(dt.status == true){
								alert("提交成功");
								location.href="dlcode.html";
								//window.location.reload(); 
							}
							//setTimeout(window.location.reload() , 2000);
						}
					});
				}else{
					//alert("提交失败")
				}
			}else if(data.status == "0"){
				alert("提交成功");
				location.href="dlcode.html";
				//window.location.reload();
				//setTimeout(window.location.reload() , 2000);
			}else{
				alert("提交失败");
			}
		}
	});

};
/**生成转发二维码**/
function qrcode(){
	
	/*******
	 * 获取转发人的opendid。
	 * 添加到URL地址参数后面，生成二维码。
	 * 用户扫描后在解析这个转发人的openid，添加项目成功后，就知道是谁转发过来的。************/
	var zf_openid =$("input[name='openid']").val();
	////////////zf_openid="o3PxO0b8G-hfmTpkVQDxqJJbMJk4";
	
	$.ajax({
		type: "post",
		url: "rest/qrcode",
		data: {
			"zf_openid":zf_openid
		},
		dataType: "json",
		success: function (d) {
			if(d.status == true){
				sessionStorage.setItem("qrcode_url",d.qrcode_url);
				location.href="mobile/qrcode.html";
			}
			else{
				alert("生成转发二维码失败");
			}
		}
	});
	
}
